package com.txw.javaCore.server.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION: hutool工具生成二维码图片
 * @Author: 涂玄武
 * @DATE: 2020/12/24 14:48
 */
@Service
@Slf4j
public class QrCodeService {

    @Autowired
    private QrConfig config;

    /**
     * 生成到文件目录
     * @param content
     * @param filePath
     * @throws Exception
     */
    public void createCodeToFile(String content, String filePath) throws Exception{
        QrCodeUtil.generate(content,config, FileUtil.file(filePath));
    }

    /**
     * 生成到流
     * @param content
     * @param response
     * @throws Exception
     */
    public void createCodeToStream(String content, HttpServletResponse response) throws Exception{
        QrCodeUtil.generate(content,config,"png",response.getOutputStream());
    }
}
